﻿
using Newtonsoft.Json;
using RTW.OA.Areas.Meeting.Servers;
using RTW.OA.Models;
using RTW.Utils.Utils;
using System;
using System.Collections.Generic;
using System.Web.Mvc;

namespace RTW.OA.Areas.Meeting.Controllers
{
    public class MeetingController : Controller
    {
        private MeetingServers meetingServers;

        public MeetingController()
        {
            meetingServers = new MeetingServers();
        }


        // GET: Meeting/Meeting
        public ActionResult Index()
        {
            ViewBag.M_ROOMData = meetingServers.GetM_ROOM(1);
            ViewBag.M_ROOMDataJS = JsonConvert.SerializeObject(meetingServers.GetM_ROOM(0));
            ViewBag.M_STATUSData = JsonConvert.SerializeObject(meetingServers.GetM_STATUS());
            ViewBag.SUMMARY_STATUSData = JsonConvert.SerializeObject(meetingServers.GetSUMMARY_STATUS());

            return View();
        }

        // GET: Meeting/Meeting/Edit/5
        public ActionResult Edit(int? id)
        {
            ViewBag.M_ROOMData = meetingServers.GetM_ROOM(0);
            var model = new meeting();
            if (id != null)
            {
                ViewBag.Title = "编辑会议表";
                model = GetModel(id.Value.ToString());
            }
            else
            {
                ViewBag.Title = "新建会议表";
            }
            return View(model);
        }


        public ActionResult MeetingDetails(int? id)
        {
            var model = new meeting();
            model = GetModel(id.Value.ToString());
            ViewBag.M_ROOMData = meetingServers.GetMR_NAME(model.M_ROOM.ToString());
            return View(model);
        }
        // POST: Meeting/Meeting/Edit/5
        [HttpPost]
        public ActionResult Edit(int? id, meeting model)
        {
            ViewBag.M_ROOMData = meetingServers.GetM_ROOM(0);
            try
            {
                if (id == null || id == 0)
                {
                    meetingServers.Add(model);
                    ViewBag.msg = "新增会议表信息成功！";
                    model = new meeting();

                }
                else
                {
                    meetingServers.Update(model);
                    ViewBag.msg = "修改会议表信息成功！";

                }
            }
            catch
            {
                ViewBag.msg = "提交出现错误！";
            }

            return View(model);
        }

        // GET: Meeting/Meeting/Delete/5
        public ActionResult Delete(int id)
        {
            if (id != 0)
            {
                meetingServers.Del(id.ToString());
                ViewBag.msg = "删除会议表信息成功！";
            }
            return Content("");
        }


        #region //GetData
        public string GetTableJsonResult(string M_NAME, string M_PROPOSER, string M_START, string M_END, string M_ROOM)
       
           {
            var list = meetingServers.GetData(M_NAME, M_PROPOSER, M_START, M_END, M_ROOM);
            return JsonConvert.SerializeObject(list);
        }

        public meeting GetModel(string id)
        {
            var model = meetingServers.GetModelById(id);
            return model;
        }

        #endregion //GetData
    }
}